package net.xju3_4.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.JdbcUtilsV1;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.common.utils.Result;
import net.xju3_4.entity.OrderDTOEntity;
import net.xju3_4.entity.OrderEntity;
import net.xju3_4.service.AdminOrderService;
import net.xju3_4.service.impl.AdminOrderImpl;

import java.io.UnsupportedEncodingException;

@RequestMapping("/api/admin/order")
public class AdminOrderController {
    private AdminOrderService adminOrderService=new AdminOrderImpl();
    @RequestMapping("/pagelist.do")
    public void pagelist(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String pageNoStr = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        String orderIdStr = req.getParameter("orderId");

        Integer pageNo = (pageNoStr == null || pageNoStr.isEmpty()) ? 1 : Integer.parseInt(pageNoStr);
        Integer pageSize = (pageSizeStr == null || pageSizeStr.isEmpty()) ? 10 : Integer.parseInt(pageSizeStr);
        String realName = req.getParameter("realName");
        String merchantName = req.getParameter("merchantName");
        String orderId=req.getParameter("orderId");

        PageInfo<OrderDTOEntity> pageInfo = adminOrderService.PageList(realName, merchantName, orderId, pageNo, pageSize);
        if (pageInfo != null) {
            JSONUtil.printByJSON(resp, Result.success(pageInfo, "获取成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("获取失败"));
        }
    }

    //    删除订单
    @RequestMapping("/deleted.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String idStr = req.getParameter("id");
        Integer id = Integer.parseInt(idStr);
        if(adminOrderService.orderDelete(id))
            JSONUtil.printByJSON(resp,Result.success("删除成功"));
        else
            JSONUtil.printByJSON(resp,Result.fail("删除失败"));
    }
//    只能更改订单状态
    @RequestMapping("/updated.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        OrderEntity order = new OrderEntity();
        order.setId(Integer.parseInt(req.getParameter("id")));
//        order.setStatus(req.getParameter("status"));
        order.setStatus(Integer.parseInt(req.getParameter("status")));
        if(adminOrderService.orderUpdate(order)){
            JSONUtil.printByJSON(resp,Result.success("更新成功"));
        }
        else
            JSONUtil.printByJSON(resp,Result.fail("更新失败"));
    }
}
